import {useState} from "react";

// 管理员查询员工信息

interface UseEmployeeInformationResponse {
    code: number;
    data: string;
    message: string;
}

interface UseEmployeeInformation {
    employeeInformation: () => Promise<void>;
    data: string;
    error2: string | null;
}

export function useEmployeeInformation(): UseEmployeeInformation {
    const [data, setData] = useState('');
    const [error2, setError] = useState<string | null>(null);
    const token = localStorage.getItem('token');
    const employeeInformation = async (): Promise<void> => {
        setError(null);
        try {
            if (!token) {
                setError('未提供授权令牌');
                return;
            }
            const response = await fetch('http://localhost:6868/admin/employeeInformation', {
                method: 'GET',
                headers: {
                    'Authorization': token!
                },
            });
            if (!response.ok) {
                throw new Error("异常");
            }
            const responseData: UseEmployeeInformationResponse = await response.json();
            setData(responseData.data);
        } catch (error2) {
            setError((error2 as Error).message);
        }
    };

    return {employeeInformation, data, error2};
}
